package com.hontech.pastacooking.net

import com.hontech.pastacooking.app.toJson
import com.hontech.pastacooking.ext.setUInt16
import com.hontech.pastacooking.ext.setUInt32


class WriteFrame {

    companion object {

        fun make(req: Int): ByteArray {
            val buf = ByteArray(8)
            buf.setUInt16(0, HEAD)
            buf.setUInt32(2, 8)
            buf.setUInt16(6, req + SALVE_FLAG)
            return buf
        }

        fun make(req: Int, body: ByteArray): ByteArray {
            val len = 8 + body.size
            val buf = ByteArray(len)
            buf.setUInt16(0, HEAD)
            buf.setUInt32(2, len)
            buf.setUInt16(6, req + SALVE_FLAG)
            System.arraycopy(body, 0, buf, 8, body.size)
            return buf
        }

        inline fun <reified T>makeJson(req: Int, obj: T): ByteArray {
            val body = obj.toJson()
            return make(req, body)
        }

        val POLL_FRAME = make(REQ_POLL)
    }

}



